package com.manage.controller;

import com.alibaba.fastjson.JSONObject;
import com.manage.domain.City;
import com.manage.util.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @author: qhl@qian360.com
 * @Date: 2016/7/15
 */

@Controller
@RequestMapping("/hello")
public class HelloMvcController {

    private Logger logger = LoggerFactory.getLogger(HelloMvcController.class);

    /**
     * localhost:8080/hello/mvc
     * 普通控制
     * @return
     */
    @RequestMapping("/mvc")
    public String helloMvc(){
        return "home";
    }

    /**
     * localhost:8080/hello/ajax/name
     * REST风格，绑定 URL 占位符到入参
     *  ajax接口 1
     *  @return
     */
    @ResponseBody
    @RequestMapping("/ajax/{name}")
    public AjaxResult helloAjax(@PathVariable String name){
        logger.info("the msg input is {}",name);
        return AjaxResult.getOK(name);
    }

    /**
     * localhost:8080/hello/ajax2/name
     *  ajax接口 2
     *  @return
     */
    @RequestMapping("/ajax2/{name}")
    public @ResponseBody AjaxResult helloAjax2(@PathVariable String name){
        logger.info("the msg input is {}",name);
        return AjaxResult.getOK(name);
    }

    /**
     * localhost:8080/hello/ajax3/name
     *  ajax接口 3
     *  @return
     */
    @RequestMapping("/ajax3/{name}")
    public ResponseEntity<AjaxResult> helloAjax3(@PathVariable String name){
        logger.info("the msg input is {}",name);
        return new ResponseEntity<AjaxResult>(AjaxResult.getOK(name), HttpStatus.OK);
    }

    /**
     * localhost:8080/hello/autoMatch?name=浙江&id=1
     *  match automatically 自动匹配参数
     *  ajax接口 4
     */
    @RequestMapping(value = "/autoMatch" , method = RequestMethod.GET)
    @ResponseBody
    public void autoMatch(String name,int id,PrintWriter pw){
        System.out.println(name+" "+id);
        pw.write(JSONObject.toJSONString(AjaxResult.getOK(name)));
    }

    /**
     * redirect 重定向到 localhost:8080/hello/ajax/{name}
     * forward
     * @return
     */
    @RequestMapping("/redirect")
    public String redirect(){
        return "redirect:/hello/ajax/redirect";
    }

    /**
     * localhost:8080/hello/sendToFront
     * 向前台传递参数 ,前台可在Request域中取到"str"
     * @param map
     * @return
     */
    @RequestMapping(value = "/sendToFront" )
    public String sendParamToFront(Map<String,String> map){
        String str="向前台传递参数map";
        map.put("cityName",str);
        return "showCity";
    }
    /**
     * localhost:8080/hello/sendModelToFront
     * 向前台传递参数 ,前台可在Request域中取到"str"
     * @param model
     * @return
     */
    @RequestMapping(value = "/sendModelToFront" )
    public String sendModelToFront(Model model){
        String str="向前台传递参数model";
        model.addAttribute("cityName",str);
        return "showCity";
    }
    /**
     * RequestBody接收的是一个Json对象的字符串
     * @param city
     */
    @RequestMapping(value = "saveCity", method = {RequestMethod.POST })
    @ResponseBody
    public void saveUser(@RequestBody City city) {
        logger.info("city {}",city);
//        cityService.save(city);
        }


    /**
     * 传统通过request获取参数  http://localhost:8080/hello/showCity?name=3
     * @param request
     * @return
     */
    @RequestMapping(value = "showCity")
    public ModelAndView showCity(HttpServletRequest request){
        String name = request.getParameter("name").toString();
        logger.info("get city name :{}",name);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("cityName",name);
//        modelAndView.addObject("cityName", cityService.findCityById(cityId)
//                .getName());
        modelAndView.setViewName("showCity");
        return modelAndView;
    }
}